package com.lupus.persistence.sql;

import java.util.Map;

public class MysqlSqlGenerator extends SqlGenerator {
	
	protected String createQuerySql(Map map){
		StringBuilder sb = new StringBuilder(512);
		sb.append("SELECT "+map.get("fieldNames")+" FROM "+map.get("tableName")+" "+map.get("joinTables"));
		String condition = (String)map.get("condition");
		if(condition!=null&&condition.length()>0){
			sb.append(" WHERE "+condition);
		}
		String orderBy = (String)map.get("orderBy");
		if(orderBy!=null&&orderBy.length()>0){
			sb.append(" ORDER BY "+orderBy);
		}
		Object firstRecord = map.get("firstRecord");
		Object pageSize = map.get("pageSize");
		if(firstRecord!=null&&pageSize!=null){
			sb.append(" LIMIT "+firstRecord+","+pageSize);
		}
		return sb.toString();
	}
	
	protected String createInsertSql(Map map){
		StringBuilder sb = new StringBuilder(256);
		String tableName = (String)map.get("tableName");
		String fieldNames = (String)map.get("fieldNames");
		sb.append("INSERT INTO "+tableName+" SET "+fieldNames);
		return sb.toString();
	}
	
	protected String createUpdateSql(Map map){
		StringBuilder sb = new StringBuilder(256);
		sb.append("UPDATE "+map.get("tableName")+" SET "+map.get("fieldNames")+"");
		String condition = (String)map.get("condition");
		if(condition!=null&&condition.length()>0){
			sb.append(" WHERE "+condition);
		}
		return sb.toString();
	}
	
	protected String createDeleteSql(Map map){
		StringBuilder sb = new StringBuilder(256);
		sb.append("DELETE FROM "+map.get("tableName"));
		String condition = (String)map.get("condition");
		if(condition!=null&&condition.length()>0){
			sb.append(" WHERE "+condition);
		}
		return sb.toString();
	}
}
